package dryven.unittest.mocks;

import java.lang.reflect.Method;

import dryven.request.controller.paramtransform.ActionParameterSerializer;
import dryven.request.controller.paramtransform.ActionParameterTransformResult;
import dryven.request.controller.paramtransform.ActionParameterTransformer;
import dryven.request.controller.paramtransform.baseimpl.PrimitiveTypeBinder;
import dryven.request.http.Request;
import dryven.request.http.Response;

public class MockActionParameterTransformer implements
		ActionParameterTransformer {

	protected Object transformedObject;
	
	@Override
	public ActionParameterTransformResult transform(Method m,
			Object[] urlParams, ActionParameterTransformResult result,
			Request request, Response response) {
		result.setOptionalFilledParameterCount(0);
		result.setTotalOptionalParameterCount(0);
		
		int stringIndex = 0;
		Object[] params = new Object[m.getParameterTypes().length];
		for(int i = 0;i<m.getParameterTypes().length;++i) {
			Class<?> clazz = m.getParameterTypes()[i];
			if(transformType(clazz)) {
				params[i] = transformedObject;
			} else {
				params[i] = new PrimitiveTypeBinder().convertPrimitiveType(clazz, urlParams[stringIndex].toString());
				++stringIndex;
			}
		}
		
		result.setParameters(params);
		
		return result;
	}
	
	//by default fill all parameters with null
	protected boolean transformType(Class<?> clazz) {
		return true;
	}

	@Override
	public ActionParameterSerializer[] getParameterSerializers(Method m) {
		return null;
	}

}
